﻿// <copyright file="Matrix3DConverter.cs" company="Axelerate Solutions S.A.">
// Copyright (c) Axelerate Solutions S.A.  All rights reserved.
// </copyright>

namespace Axelerate.Silverlight3D.Media.Media3D
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;

    /// <summary>
    /// File name: Matrix3DConverter.cs
    /// Company: Axelerate Solutions S.A.
    /// Created By: Daniel Rojas
    /// Created Date: 10/02/2008 10:24:30 h.
    /// Description: Converts instances of other types to and from instances of Matrix3D.
    ///   <Modifications>
    ///   </Modifications>
    /// </summary>
    public class Matrix3DConverter : TypeConverter
    {
        /// <summary>
        /// Indicate if the Matrix3D can be converted from the source type
        /// </summary>
        /// <param name="context">Contextual information</param>
        /// <param name="sourceType">source type to be converter to Matrix3D</param>
        /// <returns>bool indicanting if th conversion is possible</returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType.Equals(typeof(string)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Indicate if the Matrix3D can be converted to the destination type
        /// </summary>
        /// <param name="context">Contextual information</param>
        /// <param name="sourceType">Matrix3D can be converted to destination type</param>
        /// <returns>bool indicanting if th conversion is possible</returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Try to convert the value to a Matrix3D
        /// </summary>
        /// <param name="context">context type descriptor</param>
        /// <param name="culture">Culture Info</param>
        /// <param name="value">value to be converted</param>
        /// <returns>Object that was constructed. </returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                if (value.ToString().Contains("'"))
                {
                    return Matrix3D.Parse((string)value);
                }
                else
                {
                    if (value.ToString().Trim() != "")
                    {
                        string[] vals = value.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (vals.Length == 16)
                        {
                            // The string is expressed by columns: M11, M12, M13, M14, M21, M22, M23, M24, M31, M32, M33, M34, OffsetX, OffsetY, OffsetZ, M44
                            Matrix3D result = new Matrix3D(double.Parse(vals[0]), double.Parse(vals[1]), double.Parse(vals[2]), double.Parse(vals[3]),
                                                           double.Parse(vals[4]), double.Parse(vals[5]), double.Parse(vals[6]), double.Parse(vals[7]),
                                                           double.Parse(vals[8]), double.Parse(vals[9]), double.Parse(vals[10]), double.Parse(vals[11]),
                                                           double.Parse(vals[12]), double.Parse(vals[13]), double.Parse(vals[14]), double.Parse(vals[15]));
                            return result;
                        }
                        else
                        {
                            throw new FormatException();
                        }
                    }
                    else
                    {
                        return Matrix3D.Identity;
                    }
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Try to convert Matrix3D value to the destination Type
        /// </summary>
        /// <param name="context">The ITypeDescriptorContext for this call. </param>
        /// <param name="culture">The CultureInfo which is respected when converting. </param>
        /// <param name="value">The CultureInfo which is respected when converting. </param>
        /// <param name="destinationType">The CultureInfo which is respected when converting. </param>
        /// <returns>Object that was constructed. </returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)) && value is Matrix3D)
            {
                return ((Matrix3D)value).ToString();
            }
            else
            {
                return null;
            }
        }
    }
}
